package com.tms.service.impl;

import com.tms.domain.Mailbox;
import com.tms.mapper.develop.MailboxMapper;
import com.tms.service.MailboxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * 业务实现
 *
 * @author ozj
 */
@Service
public class MailboxServiceImpl implements MailboxService {

    @Autowired
    private MailboxMapper mailboxMapper;

    @Override
    @Cacheable(cacheNames = "boxs", key = "#id", unless = "#result == null")
    public Mailbox findMailboxById(Integer id) {
        return mailboxMapper.selectByPrimaryKey(id);
    }


    @Override
    @CachePut(cacheNames = "boxs", key = "#result.id", unless = "#result == null || #result.id == 0")
    public Mailbox createMailbox(Mailbox mailbox) {
        mailboxMapper.insertSelective(mailbox);
        return mailbox;
    }

    @Override
    @CacheEvict(cacheNames = "boxs", key = "#id")
    public void deleteMailboxById(Integer id) {
        mailboxMapper.deleteByPrimaryKey(id);
    }

    @Override
    @CachePut(cacheNames = "boxs", key = "#mailbox.id")
    public Mailbox updateMailboxById(Mailbox mailbox) {
        int result = mailboxMapper.updateByPrimaryKeySelective(mailbox);
        if (result == 0) {
            throw new RuntimeException("更新失败，更新结果数为0");
        }
        return mailbox;
    }
}